<template>
	<view>
		<view v-if="show" class="position-fixed top-0 left-0 right-0 bottom-0 flex align-center justify-center bg-light" style="z-index: 1000;">
			<text class="font text-muted"></text>
		</view>
		<view style="height: 44px;"></view>
		<view class="flex align-center justify-center font-lg text-muted"
			style="margin-top: 100rpx;margin-bottom: 80rpx;">欢迎回来 </view>
		<view class="px-4">
			<input type="text" v-model='form.username' class="uni-input bg-light rounded mb-4" placeholder="请输入账号" />
			<input type="password" v-model='form.password' class="uni-input bg-light rounded mb-4"
				placeholder="请输入密码" />
			<input v-if="type==='reg'" v-model='form.repassword' type="password" value=""
				class="uni-input bg-light rounded mb-4" placeholder="请确认密码" />
			<view class="bg-main text-white flex align-center justify-center font-lg py-2 rounded-circle "
				hover-class="bg-main" @click="submit">{{type==='login'?'登 录':'注 册'}}</view>
			<view class="flex align-center justify-center pt-5">
				<view class="text-muted mx-2 font-sm" @click="changtype">
					{{type==='login'?'注 册 账 号':'去 登 录'}}
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				type: 'login',
				form: {
					username: '',
					password: '',
					repassword: ''
				},
				show:false
			}
		},
		onLoad(){
			this.show = true
			if(!this.$store.state.token){
				uni.showToast({
					title:'请先登录',
					icon:'none'
				})
				return this.show = false
			}
			uni.switchTab({
				url:'../index/index'
			})
		},
		methods: {
			changtype() {
				this.type = this.type === 'login' ? 'reg' : 'login'
			},
			submit() {
				let msg = this.type === 'login' ? '登录' : '注册'

				this.$H.post('/api/'+this.type, this.form).then(res => {
					// console.log(res);
					uni.showToast({
						title: msg + '成功',
						icon: 'none'
					})
					if (this.type === 'login') {
						this.$store.dispatch('login',res).then(result=>{
							uni.switchTab({
								url:'../index/index'
							})
						})
						this.$store.dispatch('getShareUrl')
					} else {
						this.form = {
							username: '',
							password: '',
							repassword: ''
						}
						this.changtype()
					}
				})
			}
		}
	}
</script>

<style>

</style>
